package com.lipeng.controller;

import com.lipeng.bean.Club;
import com.lipeng.bean.Shop;
import com.lipeng.service.ClubService;
import com.lipeng.utils.CopyPic;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/7.
 */
@Controller
public class ClubController {

    @Resource
    private ClubService clubService;

    @RequestMapping(value = "clublist")
    public String clubList() {
        return "clublist";
    }

    @RequestMapping(value = "clubsearch")
    public String clubSearch() {
        return "clubsearch";
    }

    @RequestMapping(value = "selectclub")
    @ResponseBody
    public Club selectClubById(@RequestParam("cid") Integer cid) {
        return clubService.selectClubById(cid);
    }

    @ResponseBody
    @RequestMapping("allclub")
    public List<Club> selectAllClub() {
        return clubService.selectAllClub();
    }

    @RequestMapping(value = "addclub")
    public String addClub(MultipartFile file, HttpServletRequest request, Model model) throws IOException {
        String name = request.getParameter("name");
        String intro = request.getParameter("intro");
        String place = request.getParameter("place");
        String activity = request.getParameter("activity");
        String cidStr = request.getParameter("cid");
        int cid = 0;
        if (cidStr!=null){
            cid = Integer.valueOf(cidStr);
        }

        Club club = new Club();
        club.setCname(name);
        club.setCintro(intro);
        club.setCplace(place);
        club.setCactivity(activity);
        club.setFile(file);
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "/Users/dllo/Desktop/frame/GameApp/src/main/webapp/images/";
        String targetPath = "/Users/dllo/Desktop/frame/GameApp/target/GameApp/images/";
        //定义 文件名
        String filename = null;
        if (club.getFile()!=null) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = club.getFile().getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);
            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            club.getFile().transferTo(new File(localPath + filename));
            CopyPic.copyFile(localPath+filename,targetPath+filename);
            //把图片的相对路径保存至数据库
            sqlPath = "/images/" + filename;

        }else {
            sqlPath = clubService.selectClubById(cid).getCpic();
        }

        club.setCpic(sqlPath);
        if (cid > 0 ) {
            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("intro", intro);
            request.getSession().setAttribute("place", place);
            request.getSession().setAttribute("activity", activity);
            request.getSession().setAttribute("pic", sqlPath);
            return "redirect:updateclub?cid=" + cid + "";
        }
        int result = clubService.insertClub(club);
        model.addAttribute("club", club);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:clublist";
    }


    @RequestMapping(value = "updateclub")
    public String updateClub(HttpServletRequest request, Model model) throws IOException {
        String name = (String) request.getSession().getAttribute("name");
        String intro = (String) request.getSession().getAttribute("intro");
        String place = (String) request.getSession().getAttribute("place");
        String activity = (String) request.getSession().getAttribute("activity");
        String pic = (String) request.getSession().getAttribute("pic");
        request.getSession().removeAttribute("name");
        request.getSession().removeAttribute("intro");
        request.getSession().removeAttribute("price");
        request.getSession().removeAttribute("activity");
        request.getSession().removeAttribute("pic");

        int cid = Integer.valueOf(request.getParameter("cid"));

        Club club = new Club();
        club.setCid(cid);
        club.setCname(name);
        club.setCintro(intro);
        club.setCplace(place);
        club.setCactivity(activity);
        club.setCpic(pic);

        int result = clubService.updateClub(club);
        model.addAttribute("club", club);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:clublist";
    }

    @RequestMapping(value = "delclub")
    public String deleteClubById(HttpServletRequest request, @RequestParam("cid") Integer cid) {
        int result = clubService.deleteClubById(cid);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:clublist";
    }

    @ResponseBody
    @RequestMapping(value = "searchclubname")
    public List<Club> searchname(@Param("name")String name){
        List<Club> clubList = clubService.selectClubByName(name);
        return clubList;
    }
}
